package com.dominator.utils.system;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.network.HttpPoster;


/**
 * 翻译工具
 * @author acer
 *
 */
 
public class TranslateTools {

	public static final String CIBA_KEY="390FB2EBD5257632C5AD800F027F90C3";
	
	/**
	 * 获取词霸接口查询翻译单词
	 * @return
	 */
	public static String getWordDetail(String word){
		Map<String, String> params=new HashMap<String,String>();
		String res = null;
		try {
			res = HttpPoster.post("http://dict-co.iciba.com/api/dictionary.php?w="+word.trim()+"&key="+CIBA_KEY, params);
			res= XmlConverUtil.xmltoJson(res);
		} catch (Exception e) {
		   System.out.println("调用词霸接口失败：原因"+e.getMessage());	
		}
		System.out.println(res);	
		return res;
	}
	
	/**
	 * 获取 音标  0英国音 1美国音 2全部
	 * @param json
	 * @return
	 */
	public static String getPhonogram(String json,int type){
		JSONObject jsonObj =(JSONObject) JSONObject.parse(json);
		String res = jsonObj.getString("ps");
		if(SystemUtils.isEmpty(res)){
			return null;
		}
		String reStrings=res.replaceAll("[\\[\\]]", "");  
		String[] array=reStrings.split(",");
		if(type>=2){
			return reStrings;
		}
		return array[type];
	}
	
	/**
	 * 获取 翻译
	 * @param json
	 * @return
	 */
	public static String getTrans(String json){
		JSONObject jsonObj =(JSONObject) JSONObject.parse(json);
		String pos = jsonObj.getString("pos");
		if(SystemUtils.isEmpty(pos)){
			return null;
		}
		pos=pos.replaceAll("[\\[\\]]", "");  
		String[] posArray=pos.split(",");
		List<String> posList = Arrays.asList(posArray); 
		String acceptation = jsonObj.getString("acceptation");
		acceptation=acceptation.replaceAll("[\\[\\]]", "");  
		String[] acceptationArray=acceptation.split(",");
		String res="";
		for(int i=0;i<posList.size();i++){
			res=res+" "+posArray[i]+acceptationArray[i];
		}
		res=res.replaceAll("\\\\n", "");
		res=res.replaceAll("\"", "");
		return res;
	}
	
	
	

	
	public static void main(String[] args) {
		//String res=TranslateTool.getPhonogram(TranslateTool.getWordDetail("work"),0);
		String res=TranslateTools.getPhonogram(TranslateTools.getWordDetail("eyesight"),1);
		
		System.out.println(res);
	}

}